

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料 SPOC PPT  Demo获取 Gitee：https:&#x2F;&#x2F;gitee.com&#x2F;ccuni&#x2F;mybatis-learn-quick-get-start 运行环境  windows10 jdk8 IDEA 2021.6 专业版 mysql 5.7 log4j 1.2.17 lombok 1.18.22 mybatis3.5.0 junit 4.13.2  学习目标 理解数据">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互">
<meta property="og:url" content="http://example.com/2022/01/13/MyBatis%20%E7%AC%94%E8%AE%B01%20%E5%9F%BA%E4%BA%8EMaven%E6%90%AD%E5%BB%BAMyBatis%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%20_%20Mapper%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89%E4%BB%A5%E5%8F%8AXML%E6%96%B9%E5%BC%8F%E6%B3%A8%E5%85%A5%20_%20%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93%20_%20SqlSession%E5%88%9B%E5%BB%BA%E6%96%B9%E5%BC%8F%20_%20%E5%9F%BA%E4%BA%8E%E6%A1%86%E6%9E%B6%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%A4%E4%BA%92/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料 SPOC PPT  Demo获取 Gitee：https:&#x2F;&#x2F;gitee.com&#x2F;ccuni&#x2F;mybatis-learn-quick-get-start 运行环境  windows10 jdk8 IDEA 2021.6 专业版 mysql 5.7 log4j 1.2.17 lombok 1.18.22 mybatis3.5.0 junit 4.13.2  学习目标 理解数据">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3c2640d747e54c74b9bc9fa366030841.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0100b2f2201b4b3bb5fbe6ffa9cfb0e2.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e4406d750af84839af63edca9712077f.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e62f3b866ab04a5f8ade1ed260040370.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c57b8d017a8048f4a2ed69f824949215.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/54d981d1d3794d20a3c2fdf5a941f6ad.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fdb0ce69015d4033be91d2aa2210a430.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/73d619be4f954043bedf1a61e1a9df98.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/898a05fee172426595307df2bc77b2e2.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2ac61809a9f3476c9e8ad11d14338dde.png#pic_center">
<meta property="article:published_time" content="2022-01-13T03:16:14.000Z">
<meta property="article:modified_time" content="2022-08-22T15:46:41.282Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="MyBatis">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/3c2640d747e54c74b9bc9fa366030841.png#pic_center">
  
  
  
  <title>MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-13 11:16" pubdate>
          2022年1月13日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          16k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          136 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="http://spoc.wzu.edu.cn/study/initplay/774666.mooc">SPOC PPT </a></p>
<h1 id="Demo获取"><a href="#Demo获取" class="headerlink" title="Demo获取"></a>Demo获取</h1><hr>
<p>Gitee：<a target="_blank" rel="noopener" href="https://gitee.com/ccuni/mybatis-learn-quick-get-start">https://gitee.com/ccuni/mybatis-learn-quick-get-start</a></p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>jdk8</li>
<li>IDEA 2021.6 专业版</li>
<li>mysql 5.7</li>
<li>log4j 1.2.17</li>
<li>lombok 1.18.22</li>
<li>mybatis3.5.0</li>
<li>junit 4.13.2</li>
</ul>
<h1 id="学习目标"><a href="#学习目标" class="headerlink" title="学习目标"></a>学习目标</h1><ul>
<li>理解数据持久化的概念和ORM原理</li>
<li>理解MyBatis的概念以及优点特性</li>
<li>搭建MyBatis环境</li>
<li>了解MyBatis与JDBC的区别与联系</li>
<li>理解核心类的作用域和生命周期</li>
<li>掌握全局配置文件结构内容</li>
</ul>
<h1 id="一、框架式开发"><a href="#一、框架式开发" class="headerlink" title="一、框架式开发"></a>一、框架式开发</h1><hr>
<h2 id="1-1-传统开发与框架开发"><a href="#1-1-传统开发与框架开发" class="headerlink" title="1.1 传统开发与框架开发"></a>1.1 传统开发与框架开发</h2><p>传统Web项目（Servlet+JSP+JDBC）缺点</p>
<ul>
<li><p>DAO层：JDBC操作繁琐、冗长</p>
</li>
<li><p>Action层：每一个请求要一个Servlet（或者一个Servlet内嵌 if … else 语句）需要手动处理请求、转发等</p>
</li>
<li><p>整个项目耦合度高（action与service、service与dao）、缺乏统一管理</p>
</li>
</ul>
<p>解决办法：<strong>框架</strong></p>
<p>特点：</p>
<p>优势</p>
<ul>
<li>不用考虑公共、细节问题</li>
<li>专心在业务实现上</li>
<li>结构统一，易于维护</li>
<li>新手易上手</li>
</ul>
<p>劣势</p>
<ul>
<li>必须遵守框架约定</li>
<li>用精准需要研读底层源码</li>
<li>技术更新、淘汰快</li>
</ul>
<h2 id="1-2-web后端框架"><a href="#1-2-web后端框架" class="headerlink" title="1.2 web后端框架"></a>1.2 web后端框架</h2><h3 id="主流框架一-Struct-JPA"><a href="#主流框架一-Struct-JPA" class="headerlink" title="主流框架一 Struct + JPA"></a>主流框架一 Struct + JPA</h3><p><strong>Struts</strong></p>
<ul>
<li>MVC设计模式的实现</li>
<li>拦截器</li>
<li>可变可重用的标签</li>
</ul>
<p><strong>JPA</strong></p>
<ul>
<li>ORM，简化数据库操作</li>
<li>DAO层</li>
</ul>
<h3 id="主流框架二-SSM"><a href="#主流框架二-SSM" class="headerlink" title="主流框架二 SSM"></a>主流框架二 SSM</h3><p><strong>Spring</strong></p>
<ul>
<li>依赖注入容器 &#x2F; AOP实现</li>
<li>声明式事务</li>
<li>简化 Java EE 应用</li>
<li>粘合剂，将许多部分组装到一起</li>
</ul>
<p><strong>Spring MVC</strong> （action层）</p>
<ul>
<li>结构最清晰的MVC Model2实现</li>
<li>高度可配置，支持多种视图技术</li>
<li>定制化开发</li>
</ul>
<p><strong>MyBatis</strong></p>
<ul>
<li>半自动化的ORM实现</li>
<li>DAO层</li>
<li>动态SQL</li>
</ul>
<h2 id="1-3-web后端框架开发区别总结"><a href="#1-3-web后端框架开发区别总结" class="headerlink" title="1.3 web后端框架开发区别总结"></a>1.3 web后端框架开发区别总结</h2><ul>
<li>Servlet 用 <code>SpringMVC</code> 替代（控制层）</li>
<li>JDBC 用<code> MyBatis</code> 替代（数据库层）</li>
<li>整个项目缺乏管理 VS 用 <code>Spring</code>管理</li>
<li>Jar 包和项目构建用 <code>Maven</code> 管理</li>
<li><code>SpringBoot</code> 敏捷开发</li>
</ul>
<p>相关概念：</p>
<ul>
<li><p>持久化是程序结构在瞬时状态和持久状态间转化的过程</p>
</li>
<li><p>ORM，全称Object Relational Mapping，即对象关系映射，是一种程序设计技术，用于实现面向对象编程里不同类型的数据之间的转换。</p>
</li>
</ul>
<h2 id="1-4-MyBatis-框架优缺点"><a href="#1-4-MyBatis-框架优缺点" class="headerlink" title="1.4 MyBatis 框架优缺点"></a>1.4 MyBatis 框架优缺点</h2><p><strong>优点</strong></p>
<ul>
<li>与JDBC相比，减少了50%以上的代码量</li>
<li>最简单的持久化框架，小巧并简单易学</li>
<li>SQL代码从程序代码中彻底分离，可重用</li>
<li>提供XML标签，支持编写动态SQL</li>
<li>提供映射标签，支持对象与数据库的ORM字段映射</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>SQL语句编写工作量大，对开发人员有一定要求</li>
<li>数据库移值性差</li>
</ul>
<h1 id="二、搭建-MyBatis-开发环境"><a href="#二、搭建-MyBatis-开发环境" class="headerlink" title="二、搭建 MyBatis 开发环境"></a>二、搭建 MyBatis 开发环境</h1><hr>
<p><strong>使用 MyBatis的开发步骤</strong></p>
<ul>
<li>创建 Maven项目，导入 MyBatis依赖</li>
<li>编写MyBatis核心配置文件 <code>configuration.xml</code></li>
<li>创建实体类 - POJO（自动生成）</li>
<li>DAO层-接口 + 对应的SQL映射文件（mapper.xml）</li>
<li>创建测试类<ul>
<li>读取核心配置文件 MyBatis - config.xml</li>
<li>创建SqlSessionFactory对象，读取配置文件</li>
<li>创建SqlSession对象</li>
<li>调用mapper文件进行数据操作</li>
</ul>
</li>
</ul>
<h2 id="2-1-创建-Maven-项目导入所需依赖"><a href="#2-1-创建-Maven-项目导入所需依赖" class="headerlink" title="2.1 创建 Maven 项目导入所需依赖"></a>2.1 创建 Maven 项目导入所需依赖</h2><p>项目结构<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/4f750ba207e94d9e9896be70ea371846.png#pic_center">https://img-blog.csdnimg.cn/4f750ba207e94d9e9896be70ea371846.png#pic_center</a> &#x3D;50%x)</p>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>MyBatisFastUse<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- Mysql 连接 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.1.37<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-comment">&lt;!-- 日志管理 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>log4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>log4j<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.17<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- lombok 自动生成无参、有参构造、get、set方法--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.18.22<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-comment">&lt;!-- MyBatis --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.mybatis<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 单元测试 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>4.13.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">build</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 指定 Maven 项目编译 java 下的 xxxxMapper.xml --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">resource</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">directory</span>&gt;</span>src/main/java<span class="hljs-tag">&lt;/<span class="hljs-name">directory</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">includes</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">include</span>&gt;</span>**/*.xml<span class="hljs-tag">&lt;/<span class="hljs-name">include</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">includes</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">resource</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">build</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>





<h2 id="2-2-编写MyBatis核心配置文件"><a href="#2-2-编写MyBatis核心配置文件" class="headerlink" title="2.2 编写MyBatis核心配置文件"></a>2.2 编写MyBatis核心配置文件</h2><ul>
<li>在source 目录下新建 resource 文件夹并标记为资源文件，用于存放配置文件</li>
</ul>
<h3 id="2-2-1-配置数据库"><a href="#2-2-1-配置数据库" class="headerlink" title="2.2.1 配置数据库"></a>2.2.1 配置数据库</h3><p>在roucese文件夹下新建 database.properties用于配置基本的数据库信息，笔者以 smbms数据库为例，在和mysql传递数据的过程中，使用unicode编码格式，并且字符集设置为utf-8。</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">driver</span>=<span class="hljs-string">com.mysql.jdbc.Driver</span><br><span class="hljs-attr">url</span>=<span class="hljs-string">jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull</span><br><span class="hljs-attr">user</span>=<span class="hljs-string">数据库用户</span><br><span class="hljs-attr">password</span>=<span class="hljs-string">数据库密码</span><br></code></pre></td></tr></table></figure>

<p>配置好后，需要在IDEA项目里配置一下数据源，否则在写Mapper配置文件时会报错。</p>
<p><img src="https://img-blog.csdnimg.cn/3c2640d747e54c74b9bc9fa366030841.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="2-2-2-配置MyBatis环境"><a href="#2-2-2-配置MyBatis环境" class="headerlink" title="2.2.2 配置MyBatis环境"></a>2.2.2 配置MyBatis环境</h3><p>在resource文件夹新建<code>mybatis-config.xml</code>用于配置mybatis环境信息</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span> ?&gt;</span><br><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">configuration</span></span><br><span class="hljs-meta"><span class="hljs-keyword">PUBLIC</span> <span class="hljs-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span><br><span class="hljs-meta"><span class="hljs-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 通过这个配置文件完成mybatis与数据库的连接 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>	<span class="hljs-comment">&lt;!-- 引入 database.properties 文件--&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">properties</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;database.properties&quot;</span>/&gt;</span><br>	<span class="hljs-comment">&lt;!-- 配置mybatis的log实现为LOG4J --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">settings</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">setting</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;logImpl&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;LOG4J&quot;</span> /&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">settings</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">environments</span> <span class="hljs-attr">default</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">environment</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>			<span class="hljs-comment">&lt;!--配置事务管理，采用JDBC的事务管理  --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">transactionManager</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;JDBC&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">transactionManager</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- POOLED:mybatis自带的数据源，JNDI:基于tomcat的数据源 --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">dataSource</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;POOLED&quot;</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driver&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;driver&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;url&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;url&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;user&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;password&#125;&quot;</span>/&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">dataSource</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">environment</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">environments</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br></code></pre></td></tr></table></figure>



<h3 id="2-2-3-配置日志文件"><a href="#2-2-3-配置日志文件" class="headerlink" title="2.2.3 配置日志文件"></a>2.2.3 配置日志文件</h3><p>在resource文件夹新建log4j.properties用于配置日志信息（可不用）</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">log4j.rootLogger</span>=<span class="hljs-string">DEBUG,CONSOLE,file</span><br><br><span class="hljs-attr">log4j.logger.cn.smbms.dao</span>=<span class="hljs-string">debug</span><br><span class="hljs-attr">log4j.logger.com.ibatis</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.com.ibatis.common.jdbc.SimpleDataSource</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.com.ibatis.common.jdbc.ScriptRunner</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.java.sql.Connection</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.java.sql.Statement</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.java.sql.PreparedStatement</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.java.sql.ResultSet</span>=<span class="hljs-string">debug </span><br><span class="hljs-attr">log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</span>=<span class="hljs-string">debug</span><br><br><span class="hljs-attr">log4j.appender.CONSOLE</span>=<span class="hljs-string">org.apache.log4j.ConsoleAppender</span><br><span class="hljs-attr">log4j.appender.Threshold</span>=<span class="hljs-string">error</span><br><span class="hljs-attr">log4j.appender.CONSOLE.Target</span>=<span class="hljs-string">System.out</span><br><span class="hljs-attr">log4j.appender.CONSOLE.layout</span>=<span class="hljs-string">org.apache.log4j.PatternLayout</span><br><span class="hljs-attr">log4j.appender.CONSOLE.layout.ConversionPattern</span>= <span class="hljs-string">[%p] %d %c - %m%n</span><br><br><br><span class="hljs-attr">log4j.appender.file</span>=<span class="hljs-string">org.apache.log4j.DailyRollingFileAppender</span><br><span class="hljs-attr">log4j.appender.file.DatePattern</span>=<span class="hljs-string">yyyy-MM-dd</span><br><span class="hljs-attr">log4j.appender.file.File</span>=<span class="hljs-string">log.log</span><br><span class="hljs-attr">log4j.appender.file.Append</span>=<span class="hljs-string">true</span><br><span class="hljs-attr">log4j.appender.file.Threshold</span>=<span class="hljs-string">error</span><br><span class="hljs-attr">log4j.appender.file.layout</span>=<span class="hljs-string">org.apache.log4j.PatternLayout</span><br><span class="hljs-attr">log4j.appender.file.layout.ConversionPattern</span>=<span class="hljs-string">%d&#123;yyyy-M-d HH:mm:ss&#125;%x[%5p](%F:%L) %m%n</span><br><br><br><span class="hljs-attr">log4j.logger.com.opensymphony.xwork2</span>=<span class="hljs-string">error  </span><br></code></pre></td></tr></table></figure>



<h2 id="2-3-创建实体类"><a href="#2-3-创建实体类" class="headerlink" title="2.3 创建实体类"></a>2.3 创建实体类</h2><p>根据User表结构创建基础的User用户表结构，后序还可以添加其他成员变量。</p>
<p>测试的User数据表</p>
<p><img src="https://img-blog.csdnimg.cn/0100b2f2201b4b3bb5fbe6ffa9cfb0e2.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>使用 lombok包的注解自动生成 无参、有参构造、get、set、toSring方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@Data</span><br></code></pre></td></tr></table></figure>



<p><code>User.java</code></p>
<p>在原有数据表的字段基础上添加一个属性Age，这样可以根据birthdata变量直接得到用户的生日，这里只需要重写getAge()方法，因为它是基于birthday的值进行变化的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.pojo;<br><br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@Data</span><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>	<span class="hljs-keyword">private</span> Integer id; <span class="hljs-comment">//id </span><br>	<span class="hljs-keyword">private</span> String userCode; <span class="hljs-comment">//用户编码</span><br>	<span class="hljs-keyword">private</span> String userName; <span class="hljs-comment">//用户名称</span><br>	<span class="hljs-keyword">private</span> String userPassword; <span class="hljs-comment">//用户密码</span><br>	<span class="hljs-keyword">private</span> Integer gender;  <span class="hljs-comment">//性别</span><br>	<span class="hljs-keyword">private</span> Date birthday;  <span class="hljs-comment">//出生日期</span><br>	<span class="hljs-keyword">private</span> String phone;   <span class="hljs-comment">//电话</span><br>	<span class="hljs-keyword">private</span> String address; <span class="hljs-comment">//地址</span><br>	<span class="hljs-keyword">private</span> Integer userRole;    <span class="hljs-comment">//用户角色</span><br>	<span class="hljs-keyword">private</span> Integer createdBy;   <span class="hljs-comment">//创建者</span><br>	<span class="hljs-keyword">private</span> Date creationDate; <span class="hljs-comment">//创建时间</span><br>	<span class="hljs-keyword">private</span> Integer modifyBy;     <span class="hljs-comment">//更新者</span><br>	<span class="hljs-keyword">private</span> Date modifyDate;   <span class="hljs-comment">//更新时间</span><br>	<br>	<span class="hljs-keyword">private</span> Integer age;<span class="hljs-comment">//年龄</span><br>	<br>	<span class="hljs-keyword">private</span> String userRoleName;    <span class="hljs-comment">//用户角色名称</span><br><br>	<span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getAge</span><span class="hljs-params">()</span> &#123;<br>		<span class="hljs-type">Date</span> <span class="hljs-variable">date</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br>		<span class="hljs-type">Integer</span> <span class="hljs-variable">age</span> <span class="hljs-operator">=</span> date.getYear()-birthday.getYear();<br>		<span class="hljs-keyword">return</span> age;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h2 id="2-4-编写DAO层接口以及映射的XML文件"><a href="#2-4-编写DAO层接口以及映射的XML文件" class="headerlink" title="2.4 编写DAO层接口以及映射的XML文件"></a>2.4 编写DAO层接口以及映射的XML文件</h2><p>以查询为例 </p>
<p><code>UserMapper.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.dao.user;<br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UserMapper</span> &#123;<br>    List&lt;User&gt; <span class="hljs-title function_">queryAllUser</span><span class="hljs-params">()</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>UserMapper.xml</code></p>
<p>Mapper配置文件的格式如下</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span> ?&gt;</span><br><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">mapper</span></span><br><span class="hljs-meta"><span class="hljs-keyword">PUBLIC</span> <span class="hljs-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span></span><br><span class="hljs-meta"><span class="hljs-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">namespace</span>=<span class="hljs-string">&quot;com.uni.dao.user.UserMapper&quot;</span>&gt;</span><br>	<br>	<span class="hljs-comment">&lt;!-- 查询所有用户 --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;queryAllUser&quot;</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">&quot;com.uni.pojo.User&quot;</span>&gt;</span><br>		select * from smbms_user<br>	<span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">mapper</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>其中&lt;?xml …&gt;的声明是固定的，其中的<code>PUBLIC</code> 引入了<code>MyBatis3</code>的配置文件格式，引入之后再写mapper标签会有提示，然后mapper标签是这个配置文件最外层的，它需要指定一个namespace属性表示对应的接口类。</p>
<p>这里笔者认为是 UserDaoImpl的效果，因为之前在以MVC架构做Web课设时，通常是Dao层有一个接口，然后有一个实现类，比如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UserDao</span>&#123;<br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryAllUser</span><span class="hljs-params">()</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>对应实现类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserDao</span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryAllUser</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-comment">//调用JDBC工具类...</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在业务层使用的时候</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">UserDao</span> <span class="hljs-variable">userDao</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UserDaoImpl</span>();<br></code></pre></td></tr></table></figure>

<p>这里利用了多态的思想，接口实现类的构造方法赋值给对应接口，这样这个接口的方法就有了作用。</p>
<p>基于这个思想，现在的<code>UserMapper.xml</code>就相当于一个Impl实现类，只不过它是直接注入到<code>UserMapper</code>接口。</p>
<p>配置好Dao层后，需要让主配置文件识别映射文件，在mybatis-config.xml添加一下内容：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mappers</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;com/uni/dao/user/UserMapper.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mapper</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mappers</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-5-测试效果"><a href="#2-5-测试效果" class="headerlink" title="2.5 测试效果"></a>2.5 测试效果</h2><p>在 &#x2F;src&#x2F;test&#x2F;java下创建测试类TestUserMapper.java</p>
<p>编写测试方法void testQueryAllUser() 并在方法上一行使用Junit库的 @Test注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni;<br><span class="hljs-keyword">import</span> com.uni.pojo.User;<br><span class="hljs-keyword">import</span> org.apache.ibatis.io.Resources;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSession;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;<br><span class="hljs-keyword">import</span> org.apache.log4j.Logger;<br><span class="hljs-keyword">import</span> org.junit.Test;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.io.InputStream;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TestUserMapper</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">logger</span> <span class="hljs-operator">=</span> Logger.getLogger(TestUserMapper.class);<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testQueryAllUser</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">resource</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br>        <span class="hljs-type">SqlSession</span> <span class="hljs-variable">sqlSession</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-comment">// 1. 获取 mybatis-config.xml 的输入流</span><br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(resource);<br>            <span class="hljs-comment">// 2. 创建SqlSessionFactory对象，完成对配置文件的读取</span><br>            <span class="hljs-type">SqlSessionFactory</span> <span class="hljs-variable">factory</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(is);<br>            <span class="hljs-comment">// 3. 创建 sqlSession</span><br>            sqlSession = factory.openSession();<br><br>            <span class="hljs-comment">// 4. 调用 Mapper文件对数据进行操作，必须先把mapper文件引入到 mybatis-config.xml中</span><br>            List&lt;User&gt; users = sqlSession.selectList(<span class="hljs-string">&quot;com.uni.dao.user.UserMapper.queryAllUser&quot;</span>);<br>            <span class="hljs-comment">// 5. 将结果打印到日志中</span><br>            logger.debug(<span class="hljs-string">&quot;UserMapperTest queryAllUser results -&gt; &quot;</span> + users);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            e.printStackTrace();<br>        &#125; <span class="hljs-keyword">finally</span>&#123;<br>            <span class="hljs-keyword">if</span>(sqlSession!=<span class="hljs-literal">null</span>)<br>                sqlSession.close();<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/e4406d750af84839af63edca9712077f.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>执行过程分析</strong></p>
<ol>
<li>在主配置文件中找映射文件</li>
<li>映射文件中找命名空间</li>
<li>命名空间中找id</li>
<li>执行id对应的sql语句</li>
<li>自动完成ORM</li>
</ol>
<h2 id="2-6-调用Mapper的优化"><a href="#2-6-调用Mapper的优化" class="headerlink" title="2.6 调用Mapper的优化"></a>2.6 调用Mapper的优化</h2><ol>
<li>创建 MyBatisUtil （类似JDBCUtils）工具类</li>
</ol>
<p><code>MyBatisUtil.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.utils;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSession;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;<br><span class="hljs-keyword">import</span> org.apache.ibatis.io.Resources;<br><span class="hljs-keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.io.InputStream;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyBatisUtil</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SqlSessionFactory factory;<br><br>    <span class="hljs-comment">/** 单例模式创建工厂 只执行一次**/</span><br>    <span class="hljs-keyword">static</span> &#123;<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">resource</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br>            <span class="hljs-comment">// 1. 获取 mybatsi-config.xml 的输入流</span><br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(resource);<br>            <span class="hljs-comment">// 2. 创建 SqlSessionFactory对象，完成对配置文件的读取</span><br>            factory = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(is);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SqlSession <span class="hljs-title function_">getSession</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> factory.openSession();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p>优化后的测试：</p>
<p><code>TestUserMapper.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testQueryAllUserSimple</span><span class="hljs-params">()</span>&#123;<br>    <span class="hljs-type">SqlSession</span> <span class="hljs-variable">sqlSession</span> <span class="hljs-operator">=</span> MyBatisUtil.getSession();<br><span class="hljs-comment">//      法一 使用sqlSession的对应方法</span><br><span class="hljs-comment">//        List&lt;User&gt; users = sqlSession.selectList(&quot;com.uni.dao.user.UserMapper.queryAllUser&quot;);</span><br><span class="hljs-comment">//      法二 使用 getMapper 反射</span><br>    List&lt;User&gt; users = sqlSession.getMapper(UserMapper.class).queryAllUser();<br>    logger.debug(users);<br>    sqlSession.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/e62f3b866ab04a5f8ade1ed260040370.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="三、MyBatis-基本要素"><a href="#三、MyBatis-基本要素" class="headerlink" title="三、MyBatis 基本要素"></a>三、MyBatis 基本要素</h1><hr>
<p><em><strong>Mybatis核心对象</strong></em></p>
<ul>
<li>SqlSessionFactoryBuilder</li>
<li>SqlSessionFactory</li>
<li>SqlSession</li>
</ul>
<p><em><strong>mybatis-config.xml 系统核心配置文件</strong></em></p>
<p><em><strong>mapper.xml SQL 映射文件</strong></em></p>
<h2 id="3-1-核心接口和类的结构"><a href="#3-1-核心接口和类的结构" class="headerlink" title="3.1 核心接口和类的结构"></a>3.1 核心接口和类的结构</h2><p><img src="https://img-blog.csdnimg.cn/c57b8d017a8048f4a2ed69f824949215.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-2-核心对象SqlSessionFacotryBuilder"><a href="#3-2-核心对象SqlSessionFacotryBuilder" class="headerlink" title="3.2 核心对象SqlSessionFacotryBuilder"></a>3.2 核心对象SqlSessionFacotryBuilder</h2><p><strong>特点：</strong></p>
<ul>
<li>用过即丢，其生命周期只存在于方法体内</li>
<li>可重用来创建多个 <code>SqlSessionFacotry </code>实例</li>
<li>负责构建<code> SqlSessionFactory</code>，并提过多个build方法的重载</li>
</ul>
<p>比如，通过读取XML文件而构造的方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">resource</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br>           <span class="hljs-comment">// 1. 获取 mybatsi-config.xml 的输入流</span><br>           <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(resource);<br>           <span class="hljs-comment">// 2. 创建 SqlSessionFactory对象，完成对配置文件的读取</span><br>           factory = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(is);<br></code></pre></td></tr></table></figure>



<h2 id="3-3-核心对象-SqlSessionFactory"><a href="#3-3-核心对象-SqlSessionFactory" class="headerlink" title="3.3 核心对象 SqlSessionFactory"></a>3.3 核心对象 SqlSessionFactory</h2><p><strong>地位：</strong> SqlSessionFacotry是每个 MyBatis应用的<strong>核心</strong></p>
<p><strong>作用：</strong> 创建 SqlSession实例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">SqlSession</span> <span class="hljs-variable">session</span> <span class="hljs-operator">=</span> sqlSessionFacotry.openSession(<span class="hljs-type">boolean</span> autoCommit);<br></code></pre></td></tr></table></figure>

<p>其中的<code>autoCommit</code>参数表示是否开启事务控制，默认为true，表示关闭。</p>
<p><strong>作用域：</strong> Application</p>
<p><strong>生命周期：</strong> 与应用的生命周期相同</p>
<p><strong>单例：</strong> 存在于整个应用运行时，并且同时只存在一个对象实例</p>
<h2 id="3-4-核心对象-SqlSession"><a href="#3-4-核心对象-SqlSession" class="headerlink" title="3.4 核心对象 SqlSession"></a>3.4 核心对象 SqlSession</h2><ul>
<li>包含了执行SQL所需的<strong>所有</strong>方法</li>
<li>对应一次数据库会话，会话结束<strong>必须</strong>关闭</li>
<li>线程级别，所以<strong>无法共享</strong></li>
</ul>
<p>使用模板</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">SqlSession</span> <span class="hljs-variable">session</span> <span class="hljs-operator">=</span> sqlSessionFacotry.openSession();<br><span class="hljs-keyword">try</span>&#123;<br>    <span class="hljs-comment">// do work...</span><br>&#125; <span class="hljs-keyword">finally</span>&#123;<br>    session.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>注：在SqlSession里可以执行多次SQL语句，但关闭SqlSession后就需要重新创建。</p>
<h3 id="3-4-1-SqlSession获取方式"><a href="#3-4-1-SqlSession获取方式" class="headerlink" title="3.4.1 SqlSession获取方式"></a>3.4.1 SqlSession获取方式</h3><p>结合之前两个重要对象，SqlSession的获取方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"> <span class="hljs-type">String</span> <span class="hljs-variable">resource</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mybatis-config.xml&quot;</span>;<br>            <span class="hljs-type">InputStream</span> <span class="hljs-variable">is</span> <span class="hljs-operator">=</span> Resources.getResourceAsStream(resource);<br>            factory = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span>().build(is);<br><br><span class="hljs-type">SqlSession</span> <span class="hljs-variable">sqlSession</span> <span class="hljs-operator">=</span> facotry.openSession();<br></code></pre></td></tr></table></figure>

<h3 id="3-4-2-SqlSession的两种使用方式"><a href="#3-4-2-SqlSession的两种使用方式" class="headerlink" title="3.4.2 SqlSession的两种使用方式"></a>3.4.2 SqlSession的两种使用方式</h3><ul>
<li>通过 SqlSession实例直接运行映射的SQL语句</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">List&lt;User&gt; users = sqlSession.selectList(<span class="hljs-string">&quot;com.uni.dao.user.UserMapper.queryAllUser&quot;</span>);<br></code></pre></td></tr></table></figure>

<ul>
<li>基于Mapper接口方式操作数据（推荐）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">List&lt;User&gt; users = sqlSession.getMapper(UserMapper.class).queryAllUser();<br></code></pre></td></tr></table></figure>

<h2 id="3-5-MyBatis系统核心配置文件"><a href="#3-5-MyBatis系统核心配置文件" class="headerlink" title="3.5 MyBatis系统核心配置文件"></a>3.5 MyBatis系统核心配置文件</h2><p><code>mybatis-config.xml</code> 系统核心配置文件</p>
<ul>
<li>configuration配置<ul>
<li>properties 可以配置在Java属性配置文件中</li>
<li>settings 修改 MyBatis 在运行时的行为方式</li>
<li>typeAliases 为Java类型命名一个别名（简称）</li>
<li>typeHanlders 类型处理器</li>
<li>objectFacotry 对象工厂</li>
<li>plugins 插件</li>
<li>environments 环境</li>
<li>environment 环境变量<ul>
<li>transactionManager 事务控制器</li>
<li>dataSrouce 数据源</li>
</ul>
</li>
<li>mappers 映射器</li>
</ul>
</li>
</ul>
<p><em><strong>注意：</strong></em> 以上配置的节点 <strong>顺序</strong> 不能 <strong>随意更改</strong></p>
<h1 id="四、MyBatis-配置文件总结"><a href="#四、MyBatis-配置文件总结" class="headerlink" title="四、MyBatis 配置文件总结"></a>四、MyBatis 配置文件总结</h1><hr>
<h2 id="4-1-properties"><a href="#4-1-properties" class="headerlink" title="4.1 properties"></a>4.1 properties</h2><p>方式一：通过外部指定的方式：database.properties，实现动态配置，可重用</p>
<ul>
<li>配置 properties的resource属性</li>
</ul>
<p><code>mybatis-config.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 引入 database.properties 文件--&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">properties</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;database.properties&quot;</span>/&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">environments</span> <span class="hljs-attr">default</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">environment</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;development&quot;</span>&gt;</span><br>			<span class="hljs-comment">&lt;!--配置事务管理，采用JDBC的事务管理  --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">transactionManager</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;JDBC&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">transactionManager</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- POOLED:mybatis自带的数据源，JNDI:基于tomcat的数据源 --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">dataSource</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;POOLED&quot;</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driver&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;driver&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;url&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;url&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;user&#125;&quot;</span>/&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;password&#125;&quot;</span>/&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">dataSource</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">environment</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">environments</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>方式二：直接配置为xml，实现动态配置</p>
<ul>
<li>配置property的name属性和value属性</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/54d981d1d3794d20a3c2fdf5a941f6ad.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>若两种方式同时使用，哪种方式优先？</p>
<p><img src="https://img-blog.csdnimg.cn/fdb0ce69015d4033be91d2aa2210a430.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>总结：resource的属性值优先级高于properties子节点配置的值</p>
<p>所以在同时配置时，是方式一优先。</p>
<h2 id="4-2-settings"><a href="#4-2-settings" class="headerlink" title="4.2 settings"></a>4.2 settings</h2><p>作用：</p>
<ul>
<li><p>修改Mybatis运行时的行为方式</p>
</li>
<li><p>主要是MyBatis的一些全局配置属性的设置</p>
</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/73d619be4f954043bedf1a61e1a9df98.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-3-typeAliases"><a href="#4-3-typeAliases" class="headerlink" title="4.3 typeAliases"></a>4.3 typeAliases</h2><ul>
<li>类型别名</li>
<li>仅仅只关联XML配置，简写冗长的Java类名</li>
</ul>
<p>比如简化包名</p>
<p><code>mybatis-config.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">typeAliases</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">typeAlias</span> <span class="hljs-attr">alias</span>=<span class="hljs-string">&quot;User&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;com.uni.pojo.USer&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">typeAliases</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>或者指定某个包下JavaBean的非限定类名</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">typeAliases</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">package</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;com.uni.pojo&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">typeAliases</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>对于<code>UserMapper.xml</code></p>
<p>使用重命名前：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;queryAllUser&quot;</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">&quot;com.uni.pojo.User&quot;</span>&gt;</span><br>	slect * from smbms_user<br><span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>使用重命名后：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java">&lt;select id=<span class="hljs-string">&quot;queryAllUser&quot;</span> resultType=<span class="hljs-string">&quot;User&quot;</span>&gt;<br>	slect * from smbms_user<br>&lt;/select&gt;<br></code></pre></td></tr></table></figure>

<h2 id="4-4-environments"><a href="#4-4-environments" class="headerlink" title="4.4 environments"></a>4.4 environments</h2><ul>
<li>表示配置 MyBatis 的多套运行环境，将SQL映射到多个不同的数据库上</li>
<li>子元素节点：environment，但是必须指定其中一个为默认运行环境（通过default指定）</li>
</ul>
<p>注：每个SqlSessionFacotry实例只能选择一个运行环境</p>
<p><img src="https://img-blog.csdnimg.cn/898a05fee172426595307df2bc77b2e2.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>子元素 transactionManager 指定事务管理器</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">&lt;transactionManager type=<span class="hljs-string">&quot;JDBC&quot;</span>/&gt;<br></code></pre></td></tr></table></figure>

<p>表示采用JDBC的事务管理，除此之外还可以选择<code>MANAGED</code>托管</p>
<ul>
<li><p>子元素dataSource</p>
<ul>
<li>dataSource元素默认使用基本的JDBC数据源接口来配置JDBC连接对象的资源（也可以指定别的数据库连接池，比如：Druid）</li>
<li>有三种内建的数据源类型</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">&lt;dataSource type=<span class="hljs-string">&quot;[UNPOOLED | POOLED | JNDI]&quot;</span>/&gt;<br></code></pre></td></tr></table></figure>

<p>比如使用 POOLED进行数据库连接：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java">&lt;dataSource type=<span class="hljs-string">&quot;POOLED&quot;</span>&gt;<br>    &lt;property name=<span class="hljs-string">&quot;driver&quot;</span> value=<span class="hljs-string">&quot;$&#123;driver&#125;&quot;</span>/&gt;<br>    &lt;property name=<span class="hljs-string">&quot;url&quot;</span> value=<span class="hljs-string">&quot;$&#123;url&#125;&quot;</span>/&gt;<br>    &lt;property name=<span class="hljs-string">&quot;username&quot;</span> value=<span class="hljs-string">&quot;$&#123;user&#125;&quot;</span>/&gt;<br>    &lt;property name=<span class="hljs-string">&quot;password&quot;</span> value=<span class="hljs-string">&quot;$&#123;password&#125;&quot;</span>/&gt;<br>&lt;/dataSource&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="4-5-mappers"><a href="#4-5-mappers" class="headerlink" title="4.5 mappers"></a>4.5 mappers</h2><ul>
<li>映射器，定义SQL映射语句</li>
<li>须在配置中引用mapper映射文件</li>
</ul>
<p>方式一：使用类资源路径获取资源</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mappers</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">&quot;com/uni/dao/user/UserMapper.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mapper</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mappers</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>方式二：使用URL获取资源（远程服务器）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mampers</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">mapper</span> <span class="hljs-attr">url</span>=<span class="hljs-string">&quot;file://.../*.xml&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mampers</span>&gt;</span><br></code></pre></td></tr></table></figure>



<h1 id="五、MyBatis-入门总结"><a href="#五、MyBatis-入门总结" class="headerlink" title="五、MyBatis 入门总结"></a>五、MyBatis 入门总结</h1><hr>
<p>MyBatis是优秀的持久层框架，用于实体类和SQL语句之间建立映射关系</p>
<h2 id="5-1-SqlSession直接映射SQL语句的缺点"><a href="#5-1-SqlSession直接映射SQL语句的缺点" class="headerlink" title="5.1 SqlSession直接映射SQL语句的缺点"></a>5.1 SqlSession直接映射SQL语句的缺点</h2><ul>
<li>耦合度高</li>
</ul>
<p>每次操作需要指定 Statement的 ID，配置文件ID变了，代码也要相应改变</p>
<ul>
<li>不利于类型检查</li>
</ul>
<p>即使ID错误，编译器也不会报错</p>
<h2 id="5-2-MyBatis基于mapper接口的使用方式"><a href="#5-2-MyBatis基于mapper接口的使用方式" class="headerlink" title="5.2 MyBatis基于mapper接口的使用方式"></a>5.2 MyBatis基于mapper接口的使用方式</h2><ul>
<li>创建 UserMapper （类似于 UserDao接口对外提供操作）</li>
<li>创建 UserMapper.xml文件</li>
<li>接口中的方法和文件名中的ID取名一致</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/2ac61809a9f3476c9e8ad11d14338dde.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-3-调用Mapper的步骤"><a href="#5-3-调用Mapper的步骤" class="headerlink" title="5.3 调用Mapper的步骤"></a>5.3 调用Mapper的步骤</h2><p>基本原理：</p>
<ul>
<li>根据接口定义的方法与映射文件进行匹配，生成代理对象（类似实现接口对象 UserDaoImpl）</li>
</ul>
<p>优点：</p>
<ul>
<li>修改、维护方便、类型检查</li>
</ul>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/90645b052edc4696a3006a12267a8d80.png#pic_center">https://img-blog.csdnimg.cn/90645b052edc4696a3006a12267a8d80.png#pic_center</a> &#x3D;80%x)</p>
<h2 id="5-4-MyBatis基本要素"><a href="#5-4-MyBatis基本要素" class="headerlink" title="5.4 MyBatis基本要素"></a>5.4 MyBatis基本要素</h2><ul>
<li><p>核心对象</p>
<ul>
<li>SqlSessionFactoryBuilder</li>
<li>SqlSessionFacotry</li>
<li>SqlSession</li>
</ul>
</li>
<li><p>SQL映射文件</p>
<ul>
<li>mapper</li>
<li>select</li>
</ul>
</li>
<li><p>系统核心配置文件</p>
<ul>
<li>properties</li>
<li>settings</li>
<li>typeAliases</li>
<li>environments</li>
<li>mappers</li>
</ul>
</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/ORM/" class="category-chain-item">ORM</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/MyBatis/">#MyBatis</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互</div>
      <div>http://example.com/2022/01/13/MyBatis 笔记1 基于Maven搭建MyBatis开发环境 _ Mapper接口定义以及XML方式注入 _ 配置文件使用总结 _ SqlSession创建方式 _ 基于框架实现数据库交互/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年1月13日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/13/Linux%20_%20Shell%E7%AC%94%E8%AE%B01%20Shell%E6%A6%82%E8%BF%B0%20_%20Linux%E7%B3%BB%E7%BB%9F%E8%87%AA%E5%B8%A6%E7%9A%84%E8%A7%A3%E6%9E%90%E5%99%A8_bin_bash%20_%20bash_sh%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%20_%20%E8%84%9A%E6%9C%AC%E4%B8%AD$n%20%E3%80%81$#%E7%AD%89%E7%89%B9%E6%AE%8A%E5%8F%98%E9%87%8F%20_%20condition%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD/" title="Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Linux _ Shell笔记1 Shell概述 _ Linux系统自带的解析器_bin_bash _ bash_sh执行脚本 _ 脚本中$n 、$#等特殊变量 _ condition条件判断</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/12/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E5%95%86%E5%9F%8E%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%89%8D%E7%AB%AFJSP%20+%20Servlet%20+%20Bootstrap5%20+%20JQuery%20+%20Echarts%20_%20%E5%90%8E%E7%AB%AF%20MVC%E6%9E%B6%E6%9E%84%20+%20JDBC%20+%20Druid%E6%95%B0%E6%8D%AE%E6%B1%A0/" title="Web项目实战 _ 商城系统v1.0 _ 前端JSP + Servlet + Bootstrap5 + JQuery + Echarts _ 后端 MVC架构 + JDBC + Druid数据池">
                        <span class="hidden-mobile">Web项目实战 _ 商城系统v1.0 _ 前端JSP + Servlet + Bootstrap5 + JQuery + Echarts _ 后端 MVC架构 + JDBC + Druid数据池</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
